<template>
  <el-container class="manage_page fillcontain">
    <el-aside width="200px" class="acide">
      <el-menu :default-active="defaultActive" unique-opened router background-color="#48576a" text-color="#fff" active-text-color="#3c82f5">
        <el-menu-item index="manage"><i class="el-icon-menu"></i>首页</el-menu-item>
        <el-submenu :index="item.id" v-for="item in SidebarList" :key="item.id">
          <template slot="title"><i :class="item.icon"></i>{{ item.name }}</template>
          <el-menu-item :index="i.path" v-for="(i, index) in item.children" :key="index">{{ i.title }}</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container>
      <!-- 头部 -->
      <el-header class="header_top">
        <!-- 面包屑 -->
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/manage' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item v-for="(item, index) in $route.meta" :key="index">{{ item }}</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 我的 -->
        <el-dropdown class="quiy_top" @command="handleCommand" menu-align="start">
          <span class="el-dropdown-link">
            <img :src="baseImgPath + adminInfo.avatar" />
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="home">首页</el-dropdown-item>
            <el-dropdown-item command="signout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <!-- 内容区域 -->
      <el-main>
        <!-- 占位符 -->
        <keep-alive>
          <router-view></router-view>
        </keep-alive>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { signout } from '../api/index'
import { baseImgPath } from '../api/env'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      baseImgPath,
      SidebarList: [
        {
          id: '2',
          icon: 'el-icon-document',
          name: '数据管理',
          children: [
            { path: 'userlist', title: '用户列表' },
            { path: 'shoplist', title: '商家列表' },
            { path: 'foodlist', title: '食品列表' },
            { path: 'orderlist', title: '订单列表' },
            { path: 'adminlist', title: '管理员列表' }
          ]
        },
        {
          id: '3',
          icon: 'el-icon-plus',
          name: '添加数据',
          children: [
            { path: 'addshop', title: '添加商铺' },
            { path: 'addgoods', title: '添加商品' }
          ]
        },
        {
          id: '4',
          icon: 'el-icon-star-on',
          name: '图表',
          children: [{ path: 'visitor', title: '用户分布' }]
        },
        {
          id: '5',
          icon: 'el-icon-edit',
          name: '编辑',
          children: [{ path: 'vueedit', title: '文本编辑' }]
        },
        {
          id: '6',
          icon: 'el-icon-setting',
          name: '设置',
          children: [{ path: 'adminset', title: '管理员设置' }]
        },
        {
          id: '7',
          icon: 'el-icon-warning',
          name: '说明',
          children: [{ path: 'explain', title: '说明' }]
        }
      ]
    }
  },
  methods: {
    async handleCommand(command) {
      console.log(command)
      if (command === 'home') {
        this.$router.push('/manage')
      } else if (command === 'signout') {
        // 退出登录请求
        const res = await signout()
        console.log(res)
        if (res.data.status !== 1) {
          return this.$message.error(res.data.success)
        }
        this.$router.push('/login')
        this.$message.success(res.data.success)
      }
    }
  },
  computed: {
    defaultActive() {
      console.log(this.$route)
      return this.$route.path.replace('/', '')
    },
    ...mapState(['adminInfo'])
  }
}
</script>

<style lang="less">
@import '../style/mixin';
.manage_page {
  background: #ffffff;
  .header_top {
    background-color: #eff2f7;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .acide {
    background-color: #324057;
    height: 650px;
  }
  .quiy_top {
    width: 40px;
    height: 40px;
    margin-right: 40px;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
}
</style>
